package com.feng.study.demo.juc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapDemo {

    public static void main(String[] args) {

        arrayListDemo();

//        hashTable();
//        synchronizedMap();
//        concurrentHashMap();
    }

    private static void arrayListDemo() {
        ArrayList<Integer> list = new ArrayList<>();
        System.out.println(list.size());
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.add(22);
        list.set(5, 11);
        System.out.println(list.size());
    }

    private static void concurrentHashMap() {
        ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put(1, "逐渐搭嘎");
        concurrentHashMap.put(2, "逐渐搭嘎2");
        String s = concurrentHashMap.get(2);

        System.out.println();
    }

    /**
     * HashTable 读写方法都加了synchronized效率低下
     * 且是对象级别的锁
     */
    private static void hashTable() {
        Hashtable<Integer, String> hashtable = new Hashtable<>();
        hashtable.put(1, "张三");
        hashtable.put(2, "张三2");
        hashtable.put(3, "张三3");
        String s = hashtable.get(1);
        System.out.println();
    }

    /**
     * SynchronizedMap
     * 方法级别的锁
     */
    private static void synchronizedMap() {
        HashMap<Integer, String> hashMap = new HashMap<>();
        Map<Integer, String> integerStringMap = Collections.synchronizedMap(hashMap);
    }
}
